package com.webbei.HAO.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.webbei.HAO.service.ChSer;

public class Update extends ActionSupport {
	private String id;
	private String username;
	private String realname;
	private File file;
	private String sex;
	private String provaice;
	private String city;
	private String address;
	private String telphone;
	private String phone;
	private ChSer ser;
	private String realpath;
	// fileContentType锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷装锟较达拷锟侥硷拷锟斤拷锟斤拷锟斤拷 ,锟斤拷锟斤拷要锟斤拷file一锟斤拷
	private String fileContentType;
	// fileFileName锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷装锟较达拷锟侥硷拷锟斤拷锟侥硷拷锟斤拷
	private String fileFileName;

	public String execute() {

		String path = ServletActionContext.getServletContext().getRealPath(
				username);
		System.out.println("FIle:" + file);
		System.out.println("myfilename:" + fileFileName);
		System.out.println("myfilecontype:" + fileContentType);
		System.out.println("path:" + path);
		// About the image information, if he didn't upload pictures, then use
		// our default pictures, if the upload,
		// then delete all the pictures before, the path to the now.

		// 锟斤拷锟斤拷图片锟斤拷息锟斤拷锟斤拷锟斤拷锟矫伙拷洗锟酵计拷锟斤拷锟绞癸拷锟斤拷锟斤拷锟侥拷系锟酵计拷锟斤拷锟斤拷锟斤拷洗锟斤拷耍锟斤拷锟缴撅拷锟街帮拷锟斤拷锟斤拷锟酵计拷锟铰凤拷锟斤拷锟轿拷锟斤拷诘摹锟�
		if (file.length() > 0) {

			try {
				File f = new File(path);
				File[] fi = f.listFiles();
				if (fi.length != 0) {
					for (File fk : fi) {
						fk.delete();
					}
				}
				realpath = path + "/" + fileFileName;
				BufferedInputStream in = new BufferedInputStream(
						new FileInputStream(file));
				BufferedOutputStream out = new BufferedOutputStream(
						new FileOutputStream(realpath));
				byte[] bu = new byte[1024];
				int len = in.read(bu);
				while (len != -1) {
					out.write(bu, 0, len);
					len = in.read(bu);
				}
				out.close();
				out.flush();
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			File f = new File(path);
			File[] fi = f.listFiles();
			realpath = path + "/" + fi[0].getName();
		}

		int userid = Integer.parseInt(id);
		boolean a = ser.update(userid, username, realname, realpath, sex,
				provaice, city, address, telphone, phone);
		return SUCCESS;
	}

	/*
	 * public void validate(){
	 * System.out.println("filecon:"+fileContentType.endsWith("jpeg"));
	 * if(!fileContentType.endsWith("image")&&!fileContentType.endsWith("gif")
	 * &&!fileContentType.endsWith("jpeg")&&!fileContentType.endsWith("bmp")&&!
	 * fileContentType.endsWith("jpg"))
	 * this.addFieldError("error","锟侥硷拷锟斤拷式锟斤拷锟斤拷确"
	 * );//锟斤拷锟斤拷锟絝ile锟斤拷前台页锟斤拷锟较碉拷name }
	 */
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getRealname() {
		return realname;
	}

	public void setRealname(String realname) {
		this.realname = realname;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getRealpath() {
		return realpath;
	}

	public void setRealpath(String realpath) {
		this.realpath = realpath;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getProvaice() {
		return provaice;
	}

	public void setProvaice(String provaice) {
		this.provaice = provaice;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getTelphone() {
		return telphone;
	}

	public void setTelphone(String telphone) {
		this.telphone = telphone;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public void setSer(ChSer ser) {
		this.ser = ser;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

}
